package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.service.IEmployeeService;
import com.alibaba.druid.filter.AutoLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/login")
    public void login(String username, String password, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            employeeService.login(username,password);
            // 如果login这个方法没有问题，跳转到部门的列表页面
            response.sendRedirect("/department/list");
        }catch (Exception e){
            e.printStackTrace();
            // 如果有问题提醒用户账号或密码不正确
            // e.getMessage获取异常信息
            System.out.println(e.getMessage());
            request.setAttribute("errorMsg",e.getMessage());
            // 请求转发到login.jsp页面
            request.getRequestDispatcher("/login.jsp").forward(request,response);

        }
    }
}
